home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tfix.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  3KB  |  102 lines

  1. //
  2. // testFix.cc : test Fix (variable length) classes
  3. //
  4.  
  5. #include <Fix.h>
  6.  
  7. void check(const char* x, Fix y) { cout << x << " = " << (y) << "\n"; }
  8.  
  9. void check(const char* x, int y) { cout << x << " = " << (y) << "\n"; }
  10.  
  11. void check(const char* x, long y) { cout << x << " = " << (y) << "\n"; }
  12.  
  13. void check(const char* x, double y) { cout << x << " = " << (y) << "\n"; }
  14.  
  15. void checkb(const char* x, const Fix y)
  16. {
  17.   cout << x << " = " << (y) << " [" << length(y) << "]"<< "\n";
  18. }
  19.  
  20.  
  21.  
  22. int main() {
  23.   cout << "Fix: identities should be displayed\n"
  24.     << "[X] displays the precision of a given value\n"
  25.     << "[*] indicates that the full precision is not used for coding reasons\n";
  26.  
  27.   Fix a;        checkb("0 [16]",a);
  28.   Fix b = .5;        checkb(".5 [16]",b);
  29.   Fix c(17,-.5);    checkb("-.5 [17]",c);
  30.   Fix d(33,.1);        checkb(".1 [33]",d);
  31.   Fix e = c;        checkb("-.5 [17]",e);
  32.  
  33.   checkb(".3 [16]",a = .3);
  34.   checkb(".5 [16]",a = b);
  35.   checkb(".1 [16]",a = d);
  36.   checkb(".1 [33*]",d = a);
  37.   checkb("-.2 [17]",c = -.2);
  38.   checkb("-.5 [17]",e);
  39.  
  40.   check(".1 [16] == .1 [33*]",a == d);
  41.   d = .1;
  42.   check(".1 [16] == .1 [33]",a == d);
  43.   check(".1 [33] != .5 [16]",d != b);
  44.   check(".1 [33] > .5 [16]",d > b);
  45.   check(".1 [33] <= -.2 [17]",d <= c);
  46.  
  47.   e = .5;
  48.   check("1073741824",mantissa(e).as_double());
  49.   check(".5",value(e));
  50.  
  51.   checkb(".5 [17]",+e);
  52.   checkb("-.5 [17]",-e);
  53.  
  54.   checkb(".1 [33] + .5 [16]",d+b);
  55.   checkb(".1 [33] - .5 [16]",d-b);
  56.   checkb(".1 [33] * .5 [16]",d*b);
  57.   checkb(".1 [33] *  3",d*3);
  58.   checkb(".1 [33] * -3",d*-3);
  59.   checkb("-.1 [33] *  3",(-d)*3);
  60.   checkb("-.1 [33] * -3",(-d)*-3);
  61.   checkb(".5 [17] * -2",e*-2);
  62.   checkb(".1 [33] % 25",d%25);
  63.   checkb(".1 [33] % -25",d%-25);
  64.   checkb(".1 [33] / .5 [16]",d/b);
  65.   checkb(".1 [33] << 1",d<<1);
  66.   checkb("-.1 [33] >> 2",(-d)>>2);
  67.  
  68.   checkb("abs(-.2)",abs(c));
  69.   checkb("abs(.2)",abs(-c));
  70.   check("sgn(-.2)",sgn(c));
  71.   check("sgn(.2)",sgn(-c));
  72.   
  73.   cout << "\nshow .1 [33]\n";
  74.   show(d);
  75.  
  76.   Fix g = .95;
  77.  
  78.   cout << "\nFix: range errors warned\n";
  79.  
  80.   Fix f = 1.1;    checkb("1.1 [16]",f);
  81.  
  82.   checkb(".5 [16] / .1 [33]",b/d);
  83.   checkb(".5 [16] / 0. [16]",b/Fix(0.));
  84.   checkb(".5 [17] * 32768",e*32768);
  85.  
  86.   cout << "\nFix: overflows saturated\n";
  87.   set_overflow_handler(Fix_overflow_saturate);
  88.  
  89.   checkb(".95 [16] + .1 [33]",g+d);
  90.   checkb("-.1 [33] - .95 [16]",-d-g);
  91.   checkb(".5 [17] * 2",e*2);
  92.  
  93.   cout << "\nFix: overflows generate warnings\n";
  94.   set_overflow_handler(Fix_overflow_warning);
  95.  
  96.   checkb(".95 [16] + .1 [33]",g+d);
  97.   checkb("-.1 [33] - .95 [16]",-d-g);
  98.   checkb(".5 [17] * 2",e*2);
  99.  
  100.   return 0;
  101. }
  102.